﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using RFIDStockManagement.BLL;

namespace RFIDStockManagement.Client
{
    public partial class MakeLicense : DevExpress.XtraEditors.XtraForm
    {
        public bool LicenseSuccess { get; set; }
        public MakeLicense()
        {
            LicenseSuccess = false;
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (File.Exists(openFileDialog.FileName))
                {
                    string strConn = LicenseUtil.GetConnection(openFileDialog.FileName);
                    if (CheckDB.VerifyDB(strConn))
                    {
                        CheckDB.SetConnection(strConn);
                        if (File.Exists(LicenseUtil.LicensePath))
                        {
                            File.Delete(LicenseUtil.LicensePath);
                        }
                        File.Copy(openFileDialog.FileName, LicenseUtil.LicensePath);
                        LicenseSuccess = true;
                        this.Close();
                    }
                    else
                    {
                        AlertForm.ShowMessage("凭证文件不准确，请重新选择");
                    }
                }
                else
                {
                    AlertForm.ShowMessage("凭证文件不存在");
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowMessage(ex.Message);
            }
        }

        private void btnSelectLicFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtLicFilePath.Text = openFileDialog.FileName;
            }
        }

        private void btnMake_Click(object sender, EventArgs e)
        {
            string strServer = string.Empty;
            string strDB = string.Empty;
            string strUser = string.Empty;
            string strPwd = string.Empty;
            try
            {
                strServer = txtServer.Text.Trim();
                if (string.IsNullOrEmpty(strServer))
                {
                    AlertForm.ShowMessage("服务器地址不能为空");
                    return;
                }
                strDB = txtDB.Text.Trim();
                if (string.IsNullOrEmpty(strDB))
                {
                    AlertForm.ShowMessage("数据库名称不能为空");
                    return;
                }
                strUser = txtUser.Text.Trim();
                if (string.IsNullOrEmpty(strUser))
                {
                    AlertForm.ShowMessage("登录名不能为空");
                    return;
                }
                strPwd = txtPassword.Text.Trim();

                string strConn = string.Format("Server={0};DataBase={1};UID={2};Pwd={3}", strServer, strDB, strUser, strPwd);
                if (CheckDB.VerifyDB(strConn))
                {
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        CheckDB.SetConnection(strConn);
                        LicenseUtil.SaveLicense(strConn);
                        if (File.Exists(saveFileDialog.FileName))
                        {
                            File.Delete(saveFileDialog.FileName);
                        }
                        File.Copy(LicenseUtil.LicensePath, saveFileDialog.FileName);
                        LicenseSuccess = true;
                        this.Close();
                    }

                }
                else
                {
                    AlertForm.ShowMessage("凭证信息不正确，请检查重设！");
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }
    }
}